/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sheep.view.Component;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.RoundRectangle2D.Float;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.ListIterator;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import sheep.controller.Workspace;
import sheep.model.Note;
import sheep.view.AgendaPanel;

/**
 *
 * @author geek
 */
public class MonthGridPanel extends JPanel{

 private int line, column;
 private int columnWidth;

  private int[] resizeArrowX, resizeArrowY; 
 private GradientPaint   p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, getHeight() - 1, new Color(100, 100, 100));
 private GradientPaint   p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, getHeight() - 3, new Color(255, 255, 255, 100)); 
 private GradientPaint   p4 = new GradientPaint(0, 1, new Color(200, 200, 0, 80), 0, getHeight() - 3, new Color(255, 255, 0, 100));
 
 private Float r2d4;
 private Shape clip1;
 private Graphics2D g2d;
 
 private final int pas = 100;
 
 private JPanel notePanel;
 
 //private Calendar currentDay;
 
 private JMenuItem ds = new JMenuItem("Nouvelle note");
 JPopupMenu popup = new JPopupMenu();
 JMenuItem es = new JMenuItem("Editer note");
 JMenuItem ss = new JMenuItem("supprimer note");
 
 
 private Workspace workSpace;
 
 public LinkedList<Note> noteList = new LinkedList<Note>();
  
 public LinkedList<Note> sortedList;
  
  GradientPaint gradient;

  public MonthGridPanel(int line, int column, Workspace newWorkSpace){

    this.workSpace = newWorkSpace;
    noteList =  workSpace.getNoteList();
    sortedList = new LinkedList<Note>();
    this.line = line;
    this.column = column;      
    setOpaque(false);
 
    resizeArrowX = new int [3]; 
    resizeArrowY = new int [3]; 
    
    popup.add(ds);
    popup.add(es); 
    popup.add(ss);
    
    es.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent e) {
       popup.setVisible(false);
        notePanel.setVisible(true);      
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) { }
      public void mouseExited(MouseEvent e) { }     
    });
     
            
    ss.addMouseListener(new MouseListener(){
        
      public void mouseClicked(MouseEvent e) {
        popup.setVisible(false); 
        System.out.println("suppression ");
 
        System.out.println("suppression " + workSpace.getSelectedNote());
        workSpace.getSelectedNote().getCalendrier().getNoteList().remove(workSpace.getSelectedNote());
        noteList.remove(workSpace.getSelectedNote());
        sortedList.remove(workSpace.getSelectedNote());
        workSpace.getNoteList().remove(workSpace.getSelectedNote());
        workSpace.setSelectedNote(null);
        
        repaint();       
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) { }
      public void mouseEntered(MouseEvent e) { }
      public void mouseExited(MouseEvent e) { }
    });
    
     ds.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent e) {
            Calendar cal = (Calendar)workSpace.getCurrentDay().clone();
        cal.add(Calendar.DAY_OF_YEAR, -cal.get(Calendar.DAY_OF_MONTH));
        cal.add(Calendar.DAY_OF_YEAR, -(cal.get(Calendar.DAY_OF_WEEK)-2));
    

        Note note3 = new Note();
        note3.setCalendrier(workSpace.getCurrentCalendrier());
        workSpace.getCurrentCalendrier().addNote(note3);
        
        int xPos = (int)(popup.getLocationOnScreen().getX()- (MonthGridPanel.this.getLocationOnScreen().getX()))/ columnWidth -1;
        int yPos = (int) (((popup.getLocationOnScreen().getY())- (MonthGridPanel.this.getLocationOnScreen().getY())-20)/(pas)) ;
            
        cal.add(Calendar.DAY_OF_YEAR, 7*yPos + xPos);
        
        
        System.out.println("New note Month" + cal.getTime().toString());
        Calendar temp2 = (Calendar)cal.clone();              
        temp2.add(Calendar.HOUR_OF_DAY, 1);
              
        note3.setDate(cal);
        note3.setEndDate(temp2);
        
        note3.setTitle("Nouvelle note");
        note3.setSelected(true);
        //note3.setDragBottomEnable(true);
        workSpace.setSelectedNote(note3);
        noteList.add(note3);
        popup.setVisible(false);   
        repaint();
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}
     
     });  
    //this.setComponentPopupMenu(popup);
        
    this.addMouseMotionListener(new MouseMotionListener(){
      int YmoveSave;
      public void mouseMoved(MouseEvent e) {
        int Ymove =e.getY()-e.getY()%(pas/5);
        int Xmove =e.getX()-e.getX()% columnWidth;
        if (YmoveSave==0 )YmoveSave= Ymove;
          if(workSpace.getSelectedNote()!=null && e.getX() >= columnWidth && e.getX() <= 8*columnWidth)
              if (workSpace.getSelectedNote().isDragTopEnable()){
                //if (e.getY() <= workSpace.getSelectedNote().getNoteZone().getY()+workSpace.getSelectedNote().getNoteZone().height -10. ){
                  //workSpace.getSelectedNote().getNoteZone().height= workSpace.getSelectedNote().getNoteZone().height - Ymove + workSpace.getSelectedNote().getNoteZone().y;
                  //workSpace.getSelectedNote().getNoteZone().y= Ymove;            
                  workSpace.getSelectedNote().getDate().add(Calendar.MINUTE, 15*(int)((Ymove- YmoveSave))/ (pas/5));

                //}
              }else if (workSpace.getSelectedNote().isDragBottomEnable() ){
                if (workSpace.getSelectedNote().getNoteZone()!= null) 
                //if (e.getY() >= workSpace.getSelectedNote().getNoteZone().getY() +20.){
                  //workSpace.getSelectedNote().getNoteZone().height= Ymove - workSpace.getSelectedNote().getNoteZone().y;         
                  workSpace.getSelectedNote().getEndDate().add(Calendar.MINUTE, 15*(int)((Ymove- YmoveSave))/ (pas/5));

               // }
              }else if (workSpace.getSelectedNote().isDragEnable()){
                int i = workSpace.getSelectedNote().getDate().get(Calendar.HOUR_OF_DAY);
                //workSpace.getSelectedNote().getNoteZone().y= Ymove;
                workSpace.getSelectedNote().getNoteZone().x= Xmove;
                workSpace.getSelectedNote().setXPos(e.getX()/ columnWidth);
                workSpace.getSelectedNote().setYPos(e.getY()/ 20);                
                workSpace.getSelectedNote().getDate().set(Calendar.DAY_OF_WEEK,((Xmove)/columnWidth+1)%7);
                workSpace.getSelectedNote().getDate().set(Calendar.HOUR_OF_DAY, (e.getY()-20)/ 40);
                workSpace.getSelectedNote().getDate().set(Calendar.MINUTE,  (int) (((e.getY()-20)% 40)<10? 0 :
                                                                              ((e.getY()-20)% 40)<20? 15: 
                                                                              ((e.getY()-20)% 40)<30? 30: 45 ));
                workSpace.getSelectedNote().getEndDate().set(Calendar.DAY_OF_WEEK,((Xmove)/columnWidth+1)%7);
                workSpace.getSelectedNote().getEndDate().set(Calendar.HOUR_OF_DAY, (int)(e.getY()
                        -20+workSpace.getSelectedNote().getNoteZone().height)/ 40);
                workSpace.getSelectedNote().getEndDate().set(Calendar.MINUTE, (int) ( 
                        ((e.getY()-20+workSpace.getSelectedNote().getNoteZone().height)% 40)<10? 0 :
                        ((e.getY()-20+workSpace.getSelectedNote().getNoteZone().height)% 40)<20? 15: 
                        ((e.getY()-20+workSpace.getSelectedNote().getNoteZone().height)% 40)<30? 30: 45 ));
              }
        YmoveSave= Ymove;
        
       // Pour les notes
      if (sortedList != null){
        ListIterator iter = sortedList.listIterator(sortedList.size());
        while(iter.hasPrevious()){
          Note testNote = (Note) iter.previous();
          Calendar temp = (Calendar)workSpace.getCurrentDay().clone();
          temp.add(Calendar.DAY_OF_YEAR, -temp.get(Calendar.DAY_OF_MONTH));
          temp.add(Calendar.DAY_OF_YEAR, -(temp.get(Calendar.DAY_OF_WEEK)-2));
          int j =testNote.getDate().get(Calendar.DAY_OF_YEAR)- temp.get(Calendar.DAY_OF_YEAR);
          if (j < 43 && j >=0 && testNote.getCalendrier().isDisplay())
            if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())

              if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                e.getX()<= testNote.getNoteZone().getX() + columnWidth &&
                 e.getY()>= testNote.getNoteZone().getY()-5 && e.getY()<= testNote.getNoteZone().getY()+ 5){
                 setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
                 break;
              } else if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                e.getX()<= testNote.getNoteZone().getX() + columnWidth &&
                 e.getY()>= testNote.getNoteZone().getY() + testNote.getNoteZone().height-5 && 
                 e.getY()<= testNote.getNoteZone().getY() + testNote.getNoteZone().height +5){
                 setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
                 break;
              } else if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                e.getX()<= testNote.getNoteZone().getX() + columnWidth &&
                 e.getY()> testNote.getNoteZone().getY()+5 && 
                 e.getY()< testNote.getNoteZone().getY()+ testNote.getNoteZone().height- 5){
                 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                 break;
              }else{
                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
              }
          }
        }        
        repaint();
      }
      
      public void mouseDragged(MouseEvent e) {}
    });
    
    this.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent e) {
         popup.setVisible(false);     
         popup.remove(es);
         popup.remove(ss);
         
         System.out.println(" day week click" + workSpace.getCurrentDay().getTime().toString());         
         
                
                if (SwingUtilities.isRightMouseButton(e) && e.getX()>= columnWidth){
                  if (workSpace.getSelectedNote()!=null){
                    if (!(workSpace.getSelectedNote().isDragEnable() || workSpace.getSelectedNote().isDragBottomEnable() || 
                        workSpace.getSelectedNote().isDragTopEnable())){
                       // Pour les notes
                      if (sortedList != null){
                        ListIterator iter = sortedList.listIterator(sortedList.size());
                        while(iter.hasPrevious()){
                          Note testNote = (Note) iter.previous();
                          Calendar temp = (Calendar)workSpace.getCurrentDay().clone();
                          temp.add(Calendar.DAY_OF_YEAR, -temp.get(Calendar.DAY_OF_MONTH));
                          temp.add(Calendar.DAY_OF_YEAR, -(temp.get(Calendar.DAY_OF_WEEK)-2));
                          int j =testNote.getDate().get(Calendar.DAY_OF_YEAR)- temp.get(Calendar.DAY_OF_YEAR);
                          if (j < 43 && j >=0 && testNote.getCalendrier().isDisplay())
                            if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())

                              if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                                e.getX()<= testNote.getNoteZone().getX() + columnWidth &&
                                 e.getY()>= testNote.getNoteZone().getY() && e.getY()<= testNote.getNoteZone().getY()+ testNote.getNoteZone().height){
                                   // On definit comme selectionnÃ©                      
                                  testNote.setSelected(true);                         
                                  workSpace.setSelectedNote(testNote);
                                  popup.add(es);               
                                  popup.add(ss);

                              } 
                          }
                        }                                                                 
                      
                        popup.setLocation((int)(MonthGridPanel.this.getLocationOnScreen().getX() + e.getX()), (int)(MonthGridPanel.this.getLocationOnScreen().getY() +e.getY()));
                        if (!popup.isVisible())
                          popup.show();
                    }
                  }else{

                  if (sortedList != null){
                    ListIterator iter = sortedList.listIterator(sortedList.size());
                    while(iter.hasPrevious()){
                      Note testNote = (Note) iter.previous();
                      Calendar temp = (Calendar)workSpace.getCurrentDay().clone();
                      temp.add(Calendar.DAY_OF_YEAR, -temp.get(Calendar.DAY_OF_MONTH));
                      temp.add(Calendar.DAY_OF_YEAR, -(temp.get(Calendar.DAY_OF_WEEK)-2));
                      int j =testNote.getDate().get(Calendar.DAY_OF_YEAR)- temp.get(Calendar.DAY_OF_YEAR);
                      if (j < 43 && j >=0 && testNote.getCalendrier().isDisplay())
                        if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())

                          if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                            e.getX()<= testNote.getNoteZone().getX() + columnWidth &&
                             e.getY()>= testNote.getNoteZone().getY() && e.getY()<= testNote.getNoteZone().getY()+ testNote.getNoteZone().height){
                               // On definit comme selectionnÃ©                      
                              testNote.setSelected(true);                         
                              workSpace.setSelectedNote(testNote);
                              popup.add(es);               
                              popup.add(ss);

                          } 
                      }
                    }                     
                    popup.setLocation((int)(MonthGridPanel.this.getLocationOnScreen().getX() + e.getX()), (int)(MonthGridPanel.this.getLocationOnScreen().getY() +e.getY()));
                    if (!popup.isVisible())
                      popup.setVisible(true);
                      
                  }
                  return;
                }
                workSpace.setSelectedNote(null);
                
        Calendar temp = (Calendar)workSpace.getCurrentDay().clone();
        temp.add(Calendar.DAY_OF_YEAR, -temp.get(Calendar.DAY_OF_MONTH));
        temp.add(Calendar.DAY_OF_YEAR, -(temp.get(Calendar.DAY_OF_WEEK)-2));
    
      
        
                // Pour les notes
              if (sortedList != null){
                ListIterator iter = sortedList.listIterator(sortedList.size());
                while(iter.hasPrevious()){
                 Note testNote = (Note) iter.previous();
                 int j =testNote.getDate().get(Calendar.DAY_OF_YEAR)- temp.get(Calendar.DAY_OF_YEAR);
                 if (j < 43 && j >=0 ){
                  if (testNote.getNoteZone()!=null && testNote.getCalendrier().isDisplay())
                  
                    if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                      e.getX()<= testNote.getNoteZone().getX() + columnWidth &&
                       e.getY()>= testNote.getNoteZone().getY() &&e.getY()<= testNote.getNoteZone().getY()+ testNote.getNoteZone().height&&
                       workSpace.getSelectedNote()==null ){

                    // Double click, on ouvre la fenetre
                    if (e.getClickCount()==2){
                          testNote.setDragBottomEnable(false);
                          testNote.setDragTopEnable(false);
                          testNote.setDragEnable(false);
                          workSpace.setSelectedNote(testNote);
                          testNote.setSelected(true);
                          if (testNote.getConcurrentPos()>=3){
                            workSpace.setCurrentDay(testNote.getDate());
                            AgendaPanel.showAgendaWeek();
                          }else{
                            notePanel.setVisible(true);
                          }
                    // Simple click      
                    }else if (e.getClickCount()==1 && !testNote.isDragEnable() && !testNote.isDragTopEnable()  && !testNote.isDragBottomEnable()){                      

                          // Check le resize bord du haut
                          if (e.getY()>= testNote.getNoteZone().getY()- 5 && e.getY()<= testNote.getNoteZone().getY()+5){                         
                            testNote.setDragTopEnable(!testNote.isDragTopEnable());

                          // Check le resize bord du bas
                          }else if (e.getY()>= testNote.getNoteZone().getY()+ testNote.getNoteZone().height- 5 && 
                                  e.getY()<= testNote.getNoteZone().getY()+ testNote.getNoteZone().height+5 ){
                            testNote.setDragBottomEnable(!testNote.isDragBottomEnable());


                          // Check le deplacement
                          }else if(testNote.isSelected()){
                            testNote.setDragEnable(!testNote.isDragEnable());
                          }
                    }else{
                      testNote.setDragBottomEnable(false);
                      testNote.setDragTopEnable(false);
                      testNote.setDragEnable(false);
                      testNote.setSelected(false);    
                   }                  
                    // On definit comme selectionnÃ©
                    testNote.setSelected(true);
                    workSpace.setSelectedNote(testNote);
                  // Si on a clickÃ© hors de la note
                  }else{
                    testNote.setDragBottomEnable(false);
                    testNote.setDragTopEnable(false);
                    testNote.setDragEnable(false);
                    testNote.setSelected(false);
                  }
                }
              }
              }
                // Double click, on ouvre la fenetre
                if (e.getClickCount()==2 && workSpace.getSelectedNote()==null && 
                workSpace.getCurrentCalendrier()!=null){
                  Note note3 = new Note();
                  note3.setCalendrier(workSpace.getCurrentCalendrier());
                  
                  int xPos = (int)e.getX()/ columnWidth-1;
                  int yPos = ((int)e.getY()-20)/ (pas) ;
                   
                  Calendar cal = (Calendar)workSpace.getCurrentDay().clone();
                  cal.add(Calendar.DAY_OF_YEAR, -cal.get(Calendar.DAY_OF_MONTH));
                  cal.add(Calendar.DAY_OF_YEAR, -(cal.get(Calendar.DAY_OF_WEEK)-2));

                  cal.add(Calendar.DAY_OF_YEAR, 7*yPos + xPos);
        
 


                  Calendar temp3 = (Calendar)cal.clone();  
            
                  
                  temp3.add(Calendar.HOUR_OF_DAY, 1);

     
                  note3.setDate(cal);
                  note3.setEndDate(temp3);

                  note3.setTitle("Nouvelle note");
                  note3.setSelected(true);
                  //note3.setDragBottomEnable(true);
                  workSpace.setSelectedNote(note3);
                  noteList.add(note3);            
                  workSpace.getCurrentCalendrier().addNote(note3);
                }
               repaint();
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}      
    });
  }

  public MonthGridPanel(int line, int column, JPanel agendaNotePanel,Workspace workSpace) {
    this (line, column,workSpace);
    this.notePanel = agendaNotePanel;
  }

  
  void createGrid (Graphics g){
    
    
    Calendar temp1 = Calendar.getInstance();
    temp1.setTime(workSpace.getCurrentDay().getTime());
    temp1.add(Calendar.DAY_OF_YEAR, - temp1.get(Calendar.DAY_OF_MONTH));     
    
    for (int i = 35, j = 0; i < 740; i+=pas, j++){
      //g.setColor(Color.DARK_GRAY);
      //g.drawRoundRect(columnWidth, i-15, 7*columnWidth, 20, 8, 8);
      g.setColor(Color.LIGHT_GRAY);
      g.drawLine(0, i-15, columnWidth, i-15);
      
      
      
      g.drawString( "Semaine :" + temp1.get(Calendar.WEEK_OF_YEAR), 15, i); 
      temp1.add(Calendar.DAY_OF_YEAR, 7);
    }
    
  }  
  

  void paintNote(Note testNote, Graphics2D g2d ){
     
    Calendar cal = (Calendar)workSpace.getCurrentDay().clone();
    cal.add(Calendar.DAY_OF_YEAR, -cal.get(Calendar.DAY_OF_MONTH));
    cal.add(Calendar.DAY_OF_YEAR, -(cal.get(Calendar.DAY_OF_WEEK)-2));
    
    // Initialisation de la position de dessin
    int PosX = (((testNote.getDate().get(Calendar.DAY_OF_WEEK)+5)%7)+1 )*columnWidth;
    int longueur= pas/5;
    //Zone de dessin
    // Si la note n'a jamais Ã©tÃ© dessinÃ© ou si l'on a redimmensionnÃ©
    //if (testNote.getNoteZone()==null || testNote.getNoteZone().width!=columnWidth ){
      
      int PosY = 40+((testNote.getDate().get(Calendar.DAY_OF_YEAR)- cal.get(Calendar.DAY_OF_YEAR))/7)*pas +testNote.getConcurrentPos()*20;
      // On est prÃ©cis Ã  la demi heure

      if (testNote.isSelected())
       longueur = 2*pas/5;
      else
        longueur = pas/5;
      
      testNote.setNoteZone(new RoundRectangle2D.Float( PosX,PosY, columnWidth , longueur- 1, 20, 20));    
              
      testNote.setXPos(PosX/ columnWidth);
      testNote.setYPos( PosY);
    //}
      
      if (testNote.isSelected())
       longueur = 2*pas/5;
      else
        longueur = pas/5;
      // On impose la position de dessin
      testNote.setNoteZone(new RoundRectangle2D.Float( testNote.getNoteZone().x, 
              testNote.getNoteZone().y, columnWidth , longueur- 1, 20, 20));      
      
    gradient = new GradientPaint((int)testNote.getNoteZone().getX(),(int)testNote.getNoteZone().getY(),testNote.getCalendrier().getColor(),
                                (int)testNote.getNoteZone().getX()+ (int)(testNote.getNoteZone().getWidth()*1.4) ,
                                (int)testNote.getNoteZone().getY(),Color.WHITE);
   
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
    
    clip1 = g2d.getClip();
    
    //int effectiveWidth = columnWidth - (testNote.getConcurrentPos()*15);
    //testNote.setNoteZone(new RoundRectangle2D.Float( PosX,testNote.getNoteZone().y+testNote.getConcurrentPos()*20, columnWidth , longueur- 1, 20, 20));    
              
    //testNote.setYPos((int) testNote.getNoteZone().y+testNote.getConcurrentPos()*20);
      
    // Zone du matin
    g2d.clip(testNote.getNoteZone());
    //g2d.setPaint(p3);
    g2d.setColor(testNote.getCalendrier().getColor());
    g2d.setClip(clip1);
    g2d.fillRoundRect( testNote.getXPos()* columnWidth  ,
                      (int)(testNote.getNoteZone().y), columnWidth, (int)testNote.getNoteZone().height,18, 18);
    
    if (testNote.isSelected()){
      g2d.setColor(Color.WHITE
              );
      g2d.fillRoundRect( testNote.getXPos()* columnWidth ,
                      (int)(testNote.getNoteZone().y), columnWidth, (int)testNote.getNoteZone().height,18, 18);
    }
    // Contour foncÃ©   
    g2d.setPaint(p1);    
    g2d.drawRoundRect( testNote.getXPos()* columnWidth  , 
                      (int)(testNote.getNoteZone().y), columnWidth, (int)testNote.getNoteZone().height- 1, 20, 20);        
   
    if (testNote.isSelected())
      g2d.setColor(Color.ORANGE);
    else 
      g2d.setPaint(p2);
    
    g2d.drawRoundRect(1+ testNote.getXPos()* columnWidth  ,
                      (int)(testNote.getNoteZone().y)+1, columnWidth, (int)testNote.getNoteZone().height - 3, 18, 18);
    
    
    // Bas de la note
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    Rectangle rectClipBottom = new Rectangle (testNote.getXPos()* columnWidth , 
                              (int)(testNote.getNoteZone().y+testNote.getNoteZone().getHeight())-20, columnWidth, 20);
    g2d.clip(rectClipBottom);
    g2d.setPaint(gradient);
    g2d.fillRoundRect( testNote.getXPos()* columnWidth +1, 
                      (int)(testNote.getNoteZone().y), columnWidth-2, (int)testNote.getNoteZone().getHeight()-1,18, 18);
    if (testNote.isSelected()){
      g2d.setColor(Color.WHITE);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
      g2d.fillRoundRect( testNote.getXPos()* columnWidth +1, 
                      (int)(testNote.getNoteZone().y), columnWidth-2, (int)testNote.getNoteZone().getHeight()-1,18, 18);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    }
    
    if (testNote.isSelected())
      g2d.setColor(Color.DARK_GRAY);
    else
      g2d.setColor(Color.WHITE);
    
    g2d.setFont(new java.awt.Font("Comic Sans MS", 1, 9));
    g2d.drawString( testNote.getDate().getTime().toString().substring(11, 16) +"->"+
                    testNote.getEndDate().getTime().toString().substring(11, 16), 
                    testNote.getXPos()* columnWidth  +5,
                    (int)(testNote.getNoteZone().y+testNote.getNoteZone().getHeight())-7);
    g2d.setClip(clip1);   
    
    // Titre de la note    
    g2d.setPaint(gradient);
    Rectangle rectClip = new Rectangle (testNote.getXPos()* columnWidth ,
                      (int)(testNote.getNoteZone().y), columnWidth, 20);
    g2d.clip(rectClip);    
    g2d.fillRoundRect( testNote.getXPos()* columnWidth , 
                      (int)(testNote.getNoteZone().y), columnWidth, (int)testNote.getNoteZone().height,18, 18);
    if (testNote.isSelected()){
      g2d.setColor(Color.WHITE);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
      g2d.fillRoundRect( testNote.getXPos()* columnWidth , 
                      (int)(testNote.getNoteZone().y), columnWidth, (int)testNote.getNoteZone().height,18, 18);
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    }
    
    
    if (testNote.isSelected())
      g2d.setColor(Color.DARK_GRAY);
    else
      g2d.setColor(Color.WHITE);
    
    g2d.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
    if(testNote.getConcurrentPos() <3){
    g2d.drawString(testNote.getTitle(), testNote.getXPos()* columnWidth +5,
                    (int)(testNote.getNoteZone().y)+13);
    }else{
      g2d.drawString("...", testNote.getXPos()* columnWidth +5,
                    (int)(testNote.getNoteZone().y)+13);
    }
    g2d.setClip(clip1);
    
    //Fleche de mouvement
    if (testNote.isSelected()){
         g2d.setColor(Color.DARK_GRAY);
         g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
        if (testNote.isDragBottomEnable()){         
          int arrowPosY = (int)(testNote.getNoteZone().getY()+testNote.getNoteZone().getHeight());
          int arrowPosX = (int)(testNote.getNoteZone().getX()+ columnWidth/2.0);
          resizeArrowX[0]=arrowPosX-7;
          resizeArrowX[1]=arrowPosX;
          resizeArrowX[2]=arrowPosX+7;
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY+7;
          resizeArrowY[2]=arrowPosY;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3);
        }else if (testNote.isDragTopEnable()){
          int arrowPosY = (int)(testNote.getNoteZone().getY())+1;
          int arrowPosX = (int)(testNote.getNoteZone().getX()+ columnWidth/2.0);
          resizeArrowX[0]=arrowPosX-7;
          resizeArrowX[1]=arrowPosX;
          resizeArrowX[2]=arrowPosX+7;
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY-7;
          resizeArrowY[2]=arrowPosY;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3);          
        }else if (testNote.isDragEnable()){
          int arrowPosY = (int)(testNote.getNoteZone().getY());
          int arrowPosX = (int)(testNote.getNoteZone().getX()+ columnWidth/2.);          
          resizeArrowX[0]=arrowPosX-7;
          resizeArrowX[1]=arrowPosX;
          resizeArrowX[2]=arrowPosX+7;
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY-7;
          resizeArrowY[2]=arrowPosY;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3);  
          arrowPosY = (int)(testNote.getNoteZone().getY()+testNote.getNoteZone().getHeight());
          resizeArrowY[0]=arrowPosY;
          resizeArrowY[1]=arrowPosY+7;
          resizeArrowY[2]=arrowPosY;
          arrowPosY = (int)(testNote.getNoteZone().getY()+testNote.getNoteZone().getHeight()/2);
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3); 
          resizeArrowY[0]=arrowPosY-7;
          resizeArrowY[1]=arrowPosY;
          resizeArrowY[2]=arrowPosY+7;
          resizeArrowX[0]=arrowPosX-columnWidth/2+1;
          resizeArrowX[1]=arrowPosX-columnWidth/2-7+1;
          resizeArrowX[2]=arrowPosX-columnWidth/2+1;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3); 
          resizeArrowX[0]=arrowPosX+columnWidth/2+1;
          resizeArrowX[1]=arrowPosX+columnWidth/2+7+1;
          resizeArrowX[2]=arrowPosX+columnWidth/2+1;
          g2d.fillPolygon(resizeArrowX, resizeArrowY, 3);          
        }
    }
  }
  
  
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    columnWidth = getWidth()/column;
    // Dessin de la grille
    g.setColor(new Color(100,100,100));
    createGrid(g);
    // Positionnement en X des fleches

    // Recuperation du graphics de dessin
    g2d = (Graphics2D)g.create();
   
    clip1 = g2d.getClip();
    
    
    
        //Aujourd'hui ??
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    
  
    Calendar temp1 = Calendar.getInstance();
    temp1.setTime(workSpace.getCurrentDay().getTime());
    temp1.add(Calendar.DAY_OF_YEAR, - temp1.get(Calendar.DAY_OF_MONTH));   
       int k =Calendar.getInstance().get(Calendar.DAY_OF_YEAR) - temp1.get(Calendar.DAY_OF_YEAR);
       if (k < 43 && k >=0 ){
         
                    
          //System.out.println(" AUJOURD'HUI");
          r2d4 = new RoundRectangle2D.Float(columnWidth+columnWidth*((((temp1.get(Calendar.DAY_OF_WEEK)+5+k)%7))), 20 + pas *(k /7 ),
                  columnWidth, pas, 20, 20);
             // Zone du matin
          g2d.clip(r2d4);
          g2d.setColor(new Color(220,220, 255));
          g2d.fillRoundRect(columnWidth+columnWidth*(((temp1.get(Calendar.DAY_OF_WEEK)+5+k)%7)), 20 + pas *(k /7),
                  columnWidth-2, pas, 20, 20);
          g2d.setClip(clip1);
          //g2d.setPaint(p1);
          g2d.setColor(new Color(150,150, 255));
          g2d.drawRoundRect(columnWidth+columnWidth*(((temp1.get(Calendar.DAY_OF_WEEK)+5+k)%7)), 20 + pas *(k /7),
                  columnWidth-1, pas, 20, 20);
          //g2d.setPaint(p2);
          g2d.setColor(new Color(200,200, 255));
          g2d.drawRoundRect(columnWidth+columnWidth*(((temp1.get(Calendar.DAY_OF_WEEK)+5+k)%7)), 20 + pas *(k /7),
                  columnWidth-2, pas-1, 18, 18);
    }
       
       
       
    Calendar cal = (Calendar)workSpace.getCurrentDay().clone();
    cal.add(Calendar.DAY_OF_YEAR, -cal.get(Calendar.DAY_OF_MONTH));
    cal.add(Calendar.DAY_OF_YEAR, -(cal.get(Calendar.DAY_OF_WEEK)-2));
    
    int posx=columnWidth+5, posy=35;
    int i =0;
    g2d.setFont(new java.awt.Font("Comic Sans MS", 1, 9));
    
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    
    g2d.setColor(new Color(190,190,255));
    for (int m =0; m <6; m++){
      g2d.setColor(Color.DARK_GRAY);
      g2d.drawRoundRect(columnWidth+1, 20 +m*pas, 7*columnWidth-2, 20, 5,5);
      g2d.setColor(new Color(190,190,255));
      g2d.fillRoundRect(columnWidth+1, 20 +m*pas, 7*columnWidth-2, 20, 5,5);
    }
    
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.f));
    
    for (; cal.get(Calendar.DAY_OF_MONTH) > 7;i++){
      g2d.setColor(new Color(150,150,150));
      g2d.fillOval(posx-3, posy-14, 19, 19);
      g2d.setColor(new Color(255,255,255));
      g2d.fillOval(posx-3, posy-13, 17, 17);
      
      g2d.setColor(new Color(150,150, 150));
      g2d.drawString(""+cal.get(Calendar.DAY_OF_MONTH), posx,posy-2);
      cal.add(Calendar.DAY_OF_YEAR, 1);
      posx += columnWidth;
      if (i%7==0&& i !=0){
        posy += pas;
        posx =columnWidth+5;
      }
    }
    
    for (; cal.get(Calendar.MONTH)==workSpace.getCurrentDay().get(Calendar.MONTH) ; i++ ){
      if ( i %7==0 && i !=0){
        posy += pas;
        posx =columnWidth+5;
      }
      
      g2d.setColor(new Color(0,0,0));
      g2d.fillOval(posx-3, posy-14, 19, 19);
      g2d.setColor(new Color(255,255,255));
      g2d.fillOval(posx-2, posy-13, 17, 17);
      
      g2d.setColor(new Color(0,0,0));
      if (cal.get(Calendar.DAY_OF_MONTH)<10)
        g2d.drawString(""+cal.get(Calendar.DAY_OF_MONTH), posx+4,posy-2);
      else
        g2d.drawString(""+cal.get(Calendar.DAY_OF_MONTH), posx,posy-2);
      cal.add(Calendar.DAY_OF_YEAR, 1);
      posx += columnWidth;

    }
    
    for (; i<43 ; i++ ){
      g2d.setColor(new Color(150,150,150));
      g2d.fillOval(posx-3, posy-14, 19, 19);
      g2d.setColor(new Color(255,255,255));
      g2d.fillOval(posx-2, posy-13, 17, 17);
      
      g2d.setColor(new Color(150,150, 150));
      if (cal.get(Calendar.DAY_OF_MONTH)<10)
        g2d.drawString(""+cal.get(Calendar.DAY_OF_MONTH), posx+4,posy-2);
      else
        g2d.drawString(""+cal.get(Calendar.DAY_OF_MONTH), posx,posy -2);
      cal.add(Calendar.DAY_OF_YEAR, 1);
      posx += columnWidth;
      if ( i %7==0){
        posy += pas;
        posx =columnWidth+5;
      }
    }    
    
    
    

    

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    
    updateConcurrent();
    
    
    if (sortedList != null)
    for ( Note testNote : sortedList ){
      // temp = current day
    Calendar temp = (Calendar)workSpace.getCurrentDay().clone();
    temp.add(Calendar.DAY_OF_YEAR, -temp.get(Calendar.DAY_OF_MONTH));
    temp.add(Calendar.DAY_OF_YEAR, -(temp.get(Calendar.DAY_OF_WEEK)-2));
      int j =testNote.getDate().get(Calendar.DAY_OF_YEAR)- temp.get(Calendar.DAY_OF_YEAR);
      if (j < 43 && j >=0 && testNote.getCalendrier().isDisplay())
        if(testNote.getConcurrentPos() <4)
          paintNote(testNote,g2d);
    }
    
     if (workSpace.getSelectedNote()!=null){
       Calendar temp = (Calendar)workSpace.getCurrentDay().clone();
       int j =workSpace.getSelectedNote().getDate().get(Calendar.DAY_OF_YEAR)- temp.get(Calendar.DAY_OF_YEAR);
       if (j < 43 && j >=0 && workSpace.getSelectedNote().getCalendrier().isDisplay())        
          paintNote(workSpace.getSelectedNote(),g2d);
     }
    // On va dessiner en transparence
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
       
    g2d.dispose();
  }
  
  
  int getConcurentCout(Note note){
    ListIterator iter = sortedList.listIterator(sortedList.indexOf(note));
      while(iter.hasPrevious()){
        Note temp = (Note) iter.previous();
        if (note.getCalendrier().isDisplay() && temp.getCalendrier().isDisplay())
          if (note.getDate().get(Calendar.DAY_OF_YEAR)== temp.getDate().get(Calendar.DAY_OF_YEAR)){
              if (temp.isSelected())
                return temp.getConcurrentPos()+2;     
          return temp.getConcurrentPos()+1;              
          }
      }    
    return 0;
  }
  
  void updateConcurrent(){
    
    if (noteList.size()>0){
      sortedList = new LinkedList<Note>();
      sortedList.add(noteList.element());
      noteList.element().setConcurrent(0);
      noteList.element().setConcurrentPos(0);

      for ( Note testNote1 : noteList.subList(1, noteList.size()) ){
          // Initialisation des valeurs
          testNote1.setConcurrent(0);
          testNote1.setConcurrentPos(0);   
          // CrÃ©ation de la liste triÃ©e
          ListIterator iter = sortedList.listIterator();
          // Tant qu'il y a un element dans la list ou que celui ci est infÃ©rieur Ã  la valeur
          // et si les element graphiques sont crÃ©Ã©es
          while(iter.hasNext() && 
                  testNote1.getDate().get(Calendar.DAY_OF_YEAR)>= sortedList.get(iter.nextIndex()).getDate().get(Calendar.DAY_OF_YEAR)
                   ){
            // On avance
            iter.next();
          }
          if (testNote1.getCalendrier().isDisplay())
          iter.add(testNote1);      
      }

      // On trouve le nombre de pere de chaque note
      // (nombre maximum de note concurrente
      for ( Note testNote1 : sortedList ){        
        testNote1.setConcurrentPos(getConcurentCout(testNote1));
      }
    }
  }
  
  
}
